package icu.d4peng.basic.satoken;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.util.Assert;

import cn.dev33.satoken.config.SaSsoConfig;
import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.sso.SaSsoProcessor;
import cn.dev33.satoken.sso.SaSsoTemplate;
import cn.dev33.satoken.sso.SaSsoUtil;
import icu.d4peng.basic.satoken.properties.SaSsoProperties;

/**
 * SaSsoAutoConfiguration:sso自动配置
 *
 * @author d4peng <a href="d4peng@qq.com">d4peng@qq.com</a>
 * @since 2023.12.02
 */
@AutoConfiguration
@EnableConfigurationProperties({ SaSsoProperties.class })
@ConditionalOnClass(SaSsoManager.class)
@ConditionalOnProperty(prefix = SaSsoProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class SaSsoAutoConfiguration {

	public SaSsoAutoConfiguration(@Autowired(required = false) SaSsoConfig saSsoConfig,
			@Autowired(required = false) SaSsoTemplate ssoTemplate) {
		Assert.notNull(saSsoConfig, "saSsoConfig can't be null!");
		SaSsoManager.setConfig(saSsoConfig);
		if (ssoTemplate != null) {
			SaSsoUtil.ssoTemplate = ssoTemplate;
			SaSsoProcessor.instance.ssoTemplate = ssoTemplate;
		}
	}

}
